// let baseUrl = "http://qlapi.sylu.edu.cn/mock/1021";
let sortpage = 1;
let nextPageNumber = 456;
let thistimepage = 1;
let obj = null;
//页面传参
$(document).ready(function() {
    var para = location.href.split('?')[1];
    var id = location.href.split('=')[1];
    let qid = document.getElementById('qid')
    qid.innerText = id;
});
// 导航栏跳转部分
let token1 = localStorage.getItem("token");

// 文章分享
function article() {
    location.replace('http://127.0.0.1:5500/%E9%97%AE%E7%AD%94%E4%B8%93%E5%8C%BA/article.html');
}

//主体内容左部
//修改问题
//侦测有内容变色
$(document).ready(function() {
    $(".btn-release").attr("onclick", "");
    $("#ReleaseQuestion").bind("input propertychange", function() {
        let ReleaseQuestion = $("#ReleaseQuestion").val();

        if (ReleaseQuestion != '' && ReleaseQuestion.length >= 7 && ReleaseQuestion.length <= 50) {
            $(".btn-release").css("background", "#20A6FF");
            $(".btn-release").attr("onclick", "change()");
            // console.log(ReleaseQuestion);
        }
        if (ReleaseQuestion == '' || ReleaseQuestion.length < 7 || ReleaseQuestion.length > 50) {
            $(".btn-release").css("background", "rgba(179, 179, 179, 1)");
            $(".btn-release").attr("onclick", "");
            // console.log(ReleaseQuestion);
        }
    });

});
// 修改问题ajax
function change() {
    let qid_two = document.getElementById('qid_two')
    qid_two = qid_two.innerText;
    let question = `${$("#ReleaseQuestion").val()}`
    question = String(question);
    qid_two = Number(qid_two);
    sign = false;
    obj = {
        "question": question,
        "id": qid_two,
    };
    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/update`,
        contentType: 'application/json',
        type: "POST",
        data: JSON.stringify(obj),
        dataType: "json",
        beforeSend: function(request) {
            request.setRequestHeader("x-token", token1);
        },
        success: function(res) {
            let ReleaseQuestion = document.getElementById('ReleaseQuestion')
            ReleaseQuestion.innerText = res.data.question.question;
            alert("修改成功"); // 反馈信息
        },
        error: function(err) {
            console.log(err)
        }
    })
}

/*获取用户信息*/
$.ajax({
        url: 'http://wh2mcy.top/api/v2/api/user/home',
        contentType: 'application/x-www-form-urlencoded',
        type: 'get',
        beforeSend: function(request) {
            request.setRequestHeader("x-token", token1);
        },
        success: function(res) {
            $('#question_number').html(res.data.question_num);
            $('#answer_number').html(res.data.answer_num);
        },
        error: function() {
            console.log('error');
        },
        complete: function() {
            console.log('complete');
        }
    })
    //通过按钮切换
let page1 = document.getElementById('pageOne').innerText;
let page2 = document.getElementById('pageTwo').innerText;
let page3 = document.getElementById('pageThree').innerText;
let page4 = document.getElementById('pageFour').innerText;
let page5 = document.getElementById('pageFive').innerText;
let page6 = document.getElementById('pageSix').innerText;
let page7 = document.getElementById('pageSeven').innerText;

//翻页
let ceshi = 11321312221;
$(document).ready(function() {
    $(".turn-page-div").click(function() {

        //点击过后 不能重复点击
        $(".turn-page-div").css("background", '#FFFFFF');
        $('.turn-page-div').css('color', '#000000');
        $(this).css('background', '#20A6FF');
        $(this).css('color', 'white');
    });
});

function nextPage() {
    if (nextPageNumber == 456) {
        thistimepage = Number(thistimepage);
        thistimepage = thistimepage + 1;
        infor2(sortpage, thistimepage);
        // console.log(nextPageNumber)
        if (thistimepage % 7 == 2) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnTwo').css('background', '#20A6FF');
            $('#pageBtnTwo').css('color', 'white');
        }
        if (thistimepage % 7 == 3) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnThree').css('background', '#20A6FF');
            $('#pageBtnThree').css('color', 'white');
        }
        if (thistimepage % 7 == 4) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnFour').css('background', '#20A6FF');
            $('#pageBtnFour').css('color', 'white');
        }
        if (thistimepage % 7 == 5) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnFive').css('background', '#20A6FF');
            $('#pageBtnFive').css('color', 'white');
        }
        if (thistimepage % 7 == 6) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnSix').css('background', '#20A6FF');
            $('#pageBtnSix').css('color', 'white');
            nextpagebtn()
        }
        if (thistimepage % 7 == 0) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnSeven').css('background', '#20A6FF');
            $('#pageBtnSeven').css('color', 'white');
            nextpagebtn()
        }

    }
    if (nextPageNumber == 123) {
        page1 = Number(page1);
        page1 = page1 + 7;
        let newpage1 = document.getElementById('pageOne');
        newpage1.innerText = page1;

        page2 = Number(page2);
        page2 = page2 + 7;
        let newpage2 = document.getElementById('pageTwo');
        newpage2.innerText = page2;

        page3 = Number(page3);
        page3 = page3 + 7;
        let newpage3 = document.getElementById('pageThree');
        newpage3.innerText = page3;

        page4 = Number(page4);
        page4 = page4 + 7;
        let newpage4 = document.getElementById('pageFour');
        newpage4.innerText = page4;

        page5 = Number(page5);
        page5 = page5 + 7;
        let newpage5 = document.getElementById('pageFive');
        newpage5.innerText = page5;

        page6 = Number(page6);
        page6 = page6 + 7;
        let newpage6 = document.getElementById('pageSix');
        newpage6.innerText = page6;

        page7 = Number(page7);
        page7 = page7 + 7;
        let newpage7 = document.getElementById('pageSeven');
        newpage7.innerText = page7;
        console.log(nextPageNumber)
        $(".turn-page-div").css("background", '#FFFFFF');
        $('.turn-page-div').css('color', '#000000');
        $('#fistPageBtn').css('background', '#20A6FF');
        $('#fistPageBtn').css('color', 'white');
        infor2(sortpage, page1);
        nextpagebtn()
        if (page3 >= 10) {
            $(".turn-page-div-padding").css("padding-left", "8px");
        }
        if (page1 >= 10) {
            $(".turn-page-div-padding-two").css("padding-left", "8px");
        }
    }

}

function btncolor() {
    $(".turn-page-div").css("background", '#FFFFFF');
    $('.turn-page-div').css('color', '#000000');
    $('#fistPageBtn').css('background', '#20A6FF');
    $('#fistPageBtn').css('color', 'white');
}
//侦测是否翻页
function nextpagebtn() {
    if (page % 7 == 0) {
        // console.log(1)
        nextPageNumber = 123
            // console.log(nextPageNumber)
    } else { nextPageNumber = 456 }
}

$(document).ready(function() {
    btncolor();
});


function details() {
    let qid = document.getElementById('qid');
    qid = qid.innerText;
    qid = Number(qid)
    sign = false;
    obj = {
        "qid": qid,
    };
    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/details`,
        contentType: 'application/json',
        type: "post",
        data: JSON.stringify(obj),
        dataType: "json",
        success: function(res) {
            //获取问题
            let ReleaseQuestion = document.getElementById('ReleaseQuestion')
            ReleaseQuestion.innerText = res.data.question.question;
            //获取问题的id
            let qid_two = document.getElementById('qid_two')
            qid_two.innerText = res.data.question.id;
            //第一
            //问题本身
            let answerOne = document.getElementById('answerOne');
            let answerTimeOne = document.getElementById('answerTimeOne');
            answerOne.innerText = res.data.answer[0].comment.answer;
            answerTimeOne.innerText = res.data.answer[0].comment.creat_time;
            //用户名
            let questionOne = document.getElementById('questionOne');
            questionOne.innerText = res.data.answer[0].comment.user.username;

            //第二
            //问题本身
            let answerTwo = document.getElementById('answerTwo');
            let answerTimeTwo = document.getElementById('answerTimeTwo');
            answerTwo.innerText = res.data.answer[1].comment.answer;
            answerTimeTwo.innerText = res.data.answer[1].comment.creat_time;
            //用户名
            let questionTwo = document.getElementById('questionTwo');
            questionTwo.innerText = res.data.answer[1].comment.user.username;

            //第三
            //问题本身
            let answerThree = document.getElementById('answerThree');
            let answerTimeThree = document.getElementById('answerTimeThree');
            answerThree.innerText = res.data.answer[2].comment.answer;
            answerTimeThree.innerText = res.data.answer[2].comment.creat_time;
            //用户名
            let questionThree = document.getElementById('questionThree');
            questionThree.innerText = res.data.answer[2].comment.user.username;

            //第四
            //问题本身
            let answerFour = document.getElementById('answerFour');
            let answerTimeFour = document.getElementById('answerTimeFour');
            answerFour.innerText = res.data.answer[3].comment.answer;
            answerTimeFour.innerText = res.data.answer[3].comment.creat_time;
            //用户名
            let questionFour = document.getElementById('questionFour');
            questionFour.innerText = res.data.answer[3].comment.user.username;

            //第五
            //问题本身
            let answerFive = document.getElementById('answerFive');
            let answerTimeFive = document.getElementById('answerTimeFive');
            answerFive.innerText = res.data.answer[4].comment.answer;
            answerTimeFive.innerText = res.data.answer[4].comment.creat_time;
            //用户名
            let questionFive = document.getElementById('questionFive');
            questionFive.innerText = res.data.answer[4].comment.user.username;

            //第六
            //问题本身
            let answerSix = document.getElementById('answerSix');
            let answerTimeSix = document.getElementById('answerTimeSix');
            answerSix.innerText = res.data.answer[5].comment.answer;
            answerTimeSix.innerText = res.data.answer[5].comment.creat_time;
            //用户名
            let questionSix = document.getElementById('questionSix');
            questionSix.innerText = res.data.answer[0].comment.user.username;

            //第七
            //问题本身
            let answerSeven = document.getElementById('answerSeven');
            let answerTimeSeven = document.getElementById('answerTimeSeven');
            answerSeven.innerText = res.data.answer[6].comment.answer;
            answerTimeSeven.innerText = res.data.answer[6].comment.creat_time;
            //用户名
            let questionSeven = document.getElementById('questionSeven');
            questionSeven.innerText = res.data.answer[6].comment.user.username;
            // alert("保存成功"); // 反馈信息
        },
        error: function(err) {
            // console.log(err)
        }
    })
}
$(document).ready(function() {
    details()
});
//个人信息的点击跳转
function personal() {
    location.replace('http://wh2mcy.top/front-web/html/personal.html');
}
//取消修改的跳转
function unChange() {
    location.replace('http://wh2mcy.top/front-web/html/personal.html');
}